# ******************************************************************
#
# Copyright 2015 Samsung Electronics All Rights Reserved.
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#

import os.path
Import('env provisioning_env')

provisioning_sample_env = provisioning_env.Clone()
target_os = provisioning_sample_env.get('TARGET_OS')

######################################################################
# Build flags
######################################################################
provisioning_sample_env.AppendUnique(CPPPATH=[
    '#/resource/csdk/include',
    '#/extlibs/base64'
])

target_os = env.get('TARGET_OS')
provisioning_sample_env.AppendUnique(CPPDEFINES=['__WITH_DTLS__'])

if target_os not in ['windows']:
    provisioning_sample_env.AppendUnique(CFLAGS=['-std=c99'])
    provisioning_sample_env.AppendUnique(
        CXXFLAGS=['-std=c++0x', '-Wall', '-pthread', '-fpermissive'])
    provisioning_sample_env.AppendUnique(LIBS=['pthread'])

if target_os not in ['msys_nt', 'windows']:
    provisioning_sample_env.AppendUnique(LIBS=['dl', 'm'])
else:
    provisioning_sample_env.AppendUnique(
        LIBPATH=[os.path.join(env.get('BUILD_DIR'), 'resource', 'csdk')])

if target_os not in ['msys_nt', 'windows']:
    provisioning_sample_env.PrependUnique(LIBS=['connectivity_abstraction'])

provisioning_sample_env.PrependUnique(LIBS=['oc', 'octbstack'])

if target_os not in ['msys_nt', 'windows']:
    if provisioning_sample_env.get('WITH_TCP') == True and provisioning_sample_env.get('WITH_CLOUD') == True and provisioning_sample_env.get('SECURED') == '1':
        provisioning_sample_env.PrependUnique(
            LIBS=['oc_logger', 'coap', 'cloud', 'ocpmapi', 'ocsrm'])
    else:
        provisioning_sample_env.PrependUnique(
            LIBS=['oc_logger', 'coap', 'ocpmapi', 'ocsrm'])

if env.get('SECURED') == '1':
    provisioning_sample_env.AppendUnique(
        LIBS=['mbedtls', 'mbedx509', 'mbedcrypto'])

if env.get('MULTIPLE_OWNER') == '1':
    provisioning_sample_env.AppendUnique(CPPDEFINES=['MULTIPLE_OWNER'])

provisioning_sample_env.AppendUnique(CPPDEFINES=['TB_LOG'])

if target_os == 'tizen':
    provisioning_sample_env.AppendUnique(CPPDEFINES=['__TIZEN__'])

if target_os in ['linux', 'tizen']:
    provisioning_sample_env.ParseConfig('pkg-config --cflags --libs sqlite3')
else:
    if target_os in ['windows']:
        provisioning_sample_env.AppendUnique(LIBS=['sqlite3'])

######################################################################
# Source files and Targets
######################################################################

provisioningclient = provisioning_sample_env.Program(
    'provisioningclient', 'provisioningclient.c')
autoprovisioningclient = provisioning_sample_env.Program(
    'autoprovisioningclient', 'autoprovisioningclient.c')
sampleserver_justworks = provisioning_sample_env.Program(
    'sampleserver_justworks', 'sampleserver_justworks.cpp')
sampleserver_randompin = provisioning_sample_env.Program(
    'sampleserver_randompin', 'sampleserver_randompin.cpp')
sampleserver_mfg = provisioning_sample_env.Program(
    'sampleserver_mfg', 'sampleserver_mfg.cpp')
sampleserver_mvjustworks = provisioning_sample_env.Program(
    'sampleserver_mvjustworks', 'sampleserver_mvjustworks.cpp')
certgenerator = provisioning_sample_env.Program(
    'certgenerator', 'certgenerator.cpp')

if provisioning_sample_env.get('MULTIPLE_OWNER') == '1':
    subownerclient = provisioning_sample_env.Program(
        'subownerclient', 'subownerclient.c')
    sampleserver_preconfpin = provisioning_sample_env.Program(
        'sampleserver_preconfpin', 'sampleserver_preconfpin.cpp')

src_dir = provisioning_sample_env.get('SRC_DIR')
sec_provisioning_src_dir = src_dir + '/resource/csdk/security/provisioning/sample/'
sec_provisioning_build_dir = env.get(
    'BUILD_DIR') + '/resource/csdk/security/provisioning/sample/'


if target_os not in ['msys_nt', 'windows']:
    if provisioning_sample_env.get('WITH_TCP') == True:
        rootcert = provisioning_sample_env.Install(
            sec_provisioning_build_dir,
            sec_provisioning_src_dir + 'rootca.crt')
        if provisioning_sample_env.get('WITH_TCP') == True and provisioning_sample_env.get('WITH_CLOUD') == True and provisioning_sample_env.get('SECURED') == '1':
            Alias("cloud", [
                rootcert
            ])
            provisioning_sample_env.AppendTarget("cloud")
sampleapps = [
    provisioningclient,
    autoprovisioningclient,
    sampleserver_justworks,
    sampleserver_randompin,
    sampleserver_mfg
]
sampleapps += provisioning_sample_env.ScanJSON('resource/csdk/security/provisioning/sample')
if target_os not in ['msys_nt', 'windows']:
    if provisioning_sample_env.get('WITH_TCP') == True:
        rootcert = provisioning_sample_env.Install(
            sec_provisioning_build_dir,
            sec_provisioning_src_dir + 'rootca.crt')

Alias("samples", sampleapps)
provisioning_sample_env.AppendTarget('samples')
provisioning_sample_env.UserInstallTargetExtra(sampleapps)
